from selenium import webdriver
import time
import urllib.parse
import json
import requests
import os

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.common.keys import Keys

import data.battlenetAccount as battlenetAccount

DesiredCapabilities = webdriver.common.desired_capabilities.DesiredCapabilities

d = DesiredCapabilities.CHROME

option = webdriver.ChromeOptions()
option.add_argument('--ignore-certificate-errors')
option.add_argument('--start-maximized')
option.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
   
browser = webdriver.Chrome(options=option, desired_capabilities=d)

def start():
    login()
    gotoSubmit()
    doForum()

# 登录论坛
def login():
    # 先打开论坛
    bbsUrl = 'http://bbs.dz.blizzard.cn/forum.php'
    browser.get(bbsUrl)
    time.sleep(1)
    browser.find_elements_by_css_selector('.service-welcome a')[0].click()

    user = battlenetAccount.account.get('user')
    pwd = battlenetAccount.account.get('pwd')

    time.sleep(1)
    browser.find_element_by_id('accountName').send_keys(user)
    time.sleep(1)
    browser.find_element_by_id('password').send_keys(pwd)
    
    time.sleep(0.5)
    browser.find_element_by_id('submit').click()
    # aaa = browser.find_element_by_class_name('service-welcome')
    # aaaaa = browser.find_elements_by_css_selector('.service-welcome a:first-child')[0].click()
    # browser.find_element_by_class_name('service-welcome').click()
    time.sleep(3)

# 进入发帖页面
def gotoSubmit():
    # 军团战争专区
    # tdUrl = 'http://bbs.dz.blizzard.cn/forum.php?mod=forumdisplay&fid=68'
    tdUrl = 'http://bbs.dz.blizzard.cn/forum.php?mod=forumdisplay&fid=39&blzcmp=dzclient'
    browser.get(tdUrl)
    time.sleep(1)
    browser.find_element_by_id('newspecial').click()
    time.sleep(1)
    
# 填写内容
def doForum():
    curTime = getCurTime() 
    subject ='【 '+curTime+' 】军团战争5  赞助plus*30 申请冻结时效或者申请退款'
    content = ' \
在购买前完全没有任何提示，购买后发现完成没有效果，商品存在严重的误导说明，看论坛中就知道不是个例。\r\n\
\r\n\
申请冻结《军团战争5  赞助plus*30 》道具的时效，或者直接退款也是可以接受的。  \r\n\
\r\n\
订单号 20081022444401498458。  \r\n\
\r\n\
之前的被删掉了，这次再发一次。' + curTime + '。一直删则一直发'

    browser.find_element_by_id('subject').send_keys(subject)
    time.sleep(1)

    # start_url = "https://antirobot.tianyancha.com/captcha/verify?return_url=https%3A%2F%2Fwww.tianyancha.com%2Fsearch%2FocH-e15-s2%3Fbase%3Dhangzhou%26areaCode%3D330185&rnd="
    # driver.get(start_url)
    # onChains(driver).move_by_offset(971, 437).click().perform() # 鼠标左键点击
    # ActionChains(driver).move_by_offset(200, 100).context_click().perform() # 鼠标右键点击
    ActionChains = webdriver.ActionChains
    ActionChains(browser).move_by_offset(600, 750).click().perform() # 鼠标左键点击 进入富文本编辑区域 由于是iframe渲染的编辑区域，所以使用这种方式
    time.sleep(0.75)
    # print(content)
    addToClipBoard(content)
    ActionChains(browser).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()

    time.sleep(2)

    browser.find_element_by_id('postsubmit').click()
    
def addToClipBoard(text):
    import pyperclip
    pyperclip.copy(text)
    # command = 'echo ' + text.strip() + '| clip'
    # os.system(command)

def getCurTime():
    import time
    import locale
    locale.setlocale(locale.LC_CTYPE, 'chinese')

    curTime = time.strftime("%Y年%m月%d日 %H:%M:%S", time.localtime())
    return curTime

if __name__ == "__main__":
    #doForum()
    
    start()
    input('目测应该发帖成功，任意键退出')
    browser.close()
